package com.example.springai.controller;

import com.example.springai.tool.DateTimeTools;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import lombok.RequiredArgsConstructor;
import reactor.core.publisher.Flux;

import java.util.Objects;
import java.util.UUID;

@RestController
@RequestMapping("/chat")
class ChatController {


    private final ChatClient openApiChatClient;

    private final ChatClient dateTimeChatClient;

    public ChatController(@Qualifier("openApiChatClient") ChatClient openApiChatClient,
                          @Qualifier("dateTimeChatClient") ChatClient dateTimeChatClient) {
        this.openApiChatClient = openApiChatClient;
        this.dateTimeChatClient = dateTimeChatClient;
    }

    @GetMapping(produces = "text/html;charset=utf-8")
    public ResponseEntity<Flux<String>> chat(@RequestParam(value = "message", defaultValue = "Tell Me a joke") String message,
                                             @RequestParam(required = false, value = "conversationId") String conversationId) {
        final String finalConversationId = Objects.requireNonNullElse(conversationId, UUID.randomUUID().toString());
        return ResponseEntity.ok(openApiChatClient.prompt()
                .user(message)
                .advisors(a -> a.param(ChatMemory.CONVERSATION_ID, finalConversationId))
                .stream()
                .content());
    }

    @GetMapping("/datetime")
    public ResponseEntity<String> getDateTime(@RequestParam(value = "message", defaultValue = "What day is tomorrow?") String message,
                                              @RequestParam(required = false, value = "conversationId") String conversationId) {
        final String finalConversationId = Objects.requireNonNullElse(conversationId, UUID.randomUUID().toString());
        return ResponseEntity.ok(dateTimeChatClient.prompt(message)
                .tools(new DateTimeTools())
                .advisors(a -> a.param(ChatMemory.CONVERSATION_ID, finalConversationId))
                .call()
                .content());
    }

    @GetMapping("/alarm")
    public ResponseEntity<String> setAlarm(@RequestParam(value = "message", defaultValue = "Can you set an alarm 10 minutes from now?") String message,
                                              @RequestParam(required = false, value = "conversationId") String conversationId) {
        final String finalConversationId = Objects.requireNonNullElse(conversationId, UUID.randomUUID().toString());
        return ResponseEntity.ok(dateTimeChatClient.prompt(message)
                .tools(new DateTimeTools())
                .advisors(a -> a.param(ChatMemory.CONVERSATION_ID, finalConversationId))
                .call()
                .content());
    }

}
